package com.yinp.proapp.module.study.recycler.activity;

import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.yinp.proapp.R;
import com.yinp.proapp.adapter.ComViewHolder;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.activity.AppBaseActivity;
import com.yinp.proapp.bean.BaseBean;
import com.yinp.proapp.databinding.ActivityFlexRecyclerBinding;
import com.yinp.proapp.databinding.ItemBase2Binding;
import com.yinp.proapp.databinding.ItemGridLayoutBinding;
import com.yinp.proapp.utils.StatusBarUtil;
import com.yinp.proapp.view.GridItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: yinp
 * @Date: 2021/8/19
 * @Description: recyclerView的自适应布局
 */
public class FlexRecyclerActivity extends AppBaseActivity<ActivityFlexRecyclerBinding> {
    private List<BaseBean> flexList = new ArrayList<>();
    private CommonAdapter<BaseBean> flexAdapter;

    private List<BaseBean> gridList = new ArrayList<>();
    private CommonAdapter<BaseBean> gridAdapter;

    @Override
    protected void initViews() {
        setStatusBarHeight(StatusBarUtil.getStatusBarHeight(mContext));
        flexList.add(new BaseBean("张三", ""));
        flexList.add(new BaseBean("张三", ""));
        flexList.add(new BaseBean("张三", ""));
        flexList.add(new BaseBean("李四李四", ""));
        flexList.add(new BaseBean("李四李四", ""));
        flexList.add(new BaseBean("王五王五王五", ""));
        flexList.add(new BaseBean("张三z张三张三z张三张三z张三", ""));
        flexAdapter = new CommonAdapter<BaseBean>(mContext,flexList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemBase2Binding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, BaseBean item) {
                ItemBase2Binding binding = (ItemBase2Binding) holder.getBinding();
                binding.tvTitle.setText(item.getTitle());
                binding.tvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                binding.sllContent.setBackgroundResource(R.color.ff4d4d);
            }
        };
        FlexboxLayoutManager flm = new FlexboxLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        flm.setFlexDirection(FlexDirection.ROW);//主轴为水平方向，起点在左端。
        //flexWrap 默认情况下 Flex 跟 LinearLayout 一样，都是不带换行排列的，但是flexWrap属性可以支持换行排列。
        flm.setFlexWrap(FlexWrap.WRAP);//按正常方向换行
        //justifyContent 属性定义了项目在主轴上的对齐方式。
        flm.setJustifyContent(JustifyContent.FLEX_START);//交叉轴的起点对齐。
        bd.rvFlex.setLayoutManager(flm);
        bd.rvFlex.setAdapter(flexAdapter);
        for (int i = 0; i < 10; i++) {
            gridList.add(new BaseBean(String.valueOf(i), ""));
        }
        gridAdapter = new CommonAdapter<BaseBean>(mContext,gridList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemGridLayoutBinding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, BaseBean item) {
                ItemGridLayoutBinding binding = (ItemGridLayoutBinding) holder.getBinding();
                binding.tvTitle.setText(item.getTitle());
                binding.tvTitle.setTextColor(ContextCompat.getColor(mContext, R.color.white));
                binding.sllContent.setBackgroundResource(R.color.ff4d4d);
            }
        };
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        bd.rvGrid.setLayoutManager(gridLayoutManager);
        bd.rvGrid.setHasFixedSize(true);
        bd.rvGrid.addItemDecoration(new GridItemDecoration(mContext, new ArrayList<>(Arrays.asList(new Point(40, 20), new Point(20, 4), new Point(0, 0)))));
        bd.rvGrid.setAdapter(gridAdapter);
    }
}